package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    AliyunSmsUtils smsUtils;
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public void createSmsCode(String mobile, String type) {
//        参数合法性
        if (StrUtil.isEmpty(mobile)||StrUtil.isEmpty(type)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
//          判断手机号是否合法
        if (!PhoneNumCheckUtils.isPhoneLegal(mobile)){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }

//        注册时手机号不能存在
        if (type.equals("2")){
           Boolean flag= sysUserService.checkExists(mobile);
           if (flag){
               throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
           }
        }else {
//      其它情况下手机号必须存在
            Boolean flag=sysUserService.checkExists(mobile);
            if (!flag){
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }

//        生成随机验证吗
          String code = RandomUtil.randomNumbers(6);

//        TODO 验证码欠费
//        发送验证码
        /*Boolean sms = smsUtils.sendSMS(mobile, type);
        if (!sms){
            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
        }*/
//        将验证码存入Redis
//        组装key值，将验证码存入
        String key=getPrefix(type)+mobile;
        redisTemplate.boundValueOps(key).set(code,5L, TimeUnit.MINUTES);

        log.info("成功发送验证码 , 具体信息 : " + mobile+", " + code);
    }

    String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            default:
                return null;
        }
    }

    @Override
    public Boolean verify(String mobile, String type, String checkcode) {
//        获取redis中验证码 与当前验证码较验
        //校验参数
        if(StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type) || StrUtil.isEmpty(checkcode)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        String key=this.getPrefix(type)+mobile;
        String redisCode = redisTemplate.boundValueOps(key).get();

        return StrUtil.isNotEmpty(redisCode) && redisCode.equals(redisCode);
    }

    @Override
    public void deleteSmsCode(String mobile, String type) {
        String redisKey = this.getPrefix(type)+mobile;
        redisTemplate.delete(redisKey);
    }
}
